﻿using System;
using System.ComponentModel;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;

namespace SyncSPW.ListSyncPart
{
    [ToolboxItemAttribute(false)]
    public class ListSyncPart : WebPart
    {
        protected override void RenderContents(HtmlTextWriter writer)
        {
            SPWeb web = SPContext.Current.Web;

            writer.Write("<script type='text/javascript'>\r\n");
            writer.Write("  function syncListSPW(listID)\r\n");
            writer.Write("  {\r\n");
            writer.Write("    var OfflineClientScope_Site = 1;\r\n");
            writer.Write("    var OfflineClientScope_ListOrLibrary = 2;\r\n");
            writer.Write("    var OfflineClientScope_Folder = 3;\r\n");
            writer.Write("    var url = '");
            writer.Write(web.Url);
            writer.Write("';\r\n");
            writer.Write("    var a = new ActiveXObject('Groove.SiteClientActiveX');\r\n");
            writer.Write("    if(a.IsOfflineAllowed(OfflineClientScope_ListOrLibrary, 100, 100, 100))\r\n");
            writer.Write("      a.TakeOffline(OfflineClientScope_ListOrLibrary, 1, url, 1, 1, listID, '');\r\n");
            writer.Write("    else\r\n");
            writer.Write("      alert('List cannot be taken offline.');\r\n");
            writer.Write("  }\r\n");
            writer.Write("</script>\r\n");

            writer.Write("<div>\r\n");
            foreach (SPList list in web.Lists)
            {
                writer.Write("<a href=\"javascript:syncListSPW('");
                writer.Write(list.ID);
                writer.Write("');\">Sync ");
                writer.Write(list.Title);
                writer.Write("</a><br/>\r\n");
                web.Dispose();
            }
            writer.Write("</div>\r\n");
        }
    }
}
